package com.mumu.quratz.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mumu.quratz.entity.SimpleProperty;
import com.mumu.quratz.entity.SysJob;
import com.mumu.quratz.util.ScheduleUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TimeOfDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;
import java.util.Date;

/**
 * @author mumu
 * @version 1.0
 * @date 2023/12/12 17:14
 * @describe
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private Scheduler scheduler;


    @GetMapping("test")
    public void test() throws SchedulerException {
        SysJob sysJob = new SysJob();
        sysJob.setJobId("1");
        sysJob.setJobName("name");
        sysJob.setJobGroup("groupName");
        sysJob.setTargetClass("testJob");

        ObjectMapper objectMapper = new ObjectMapper();

        // 创建一个空的 JSON 对象
        ObjectNode jsonNode = objectMapper.createObjectNode();

        // 添加属性到 JSON 对象
        jsonNode.put("name", "John Doe");
        jsonNode.put("age", 30);
        jsonNode.put("isStudent", false);

        LocalTime currentTime = LocalTime.now().plusSeconds(10L);
        log.info("执行时间---{}", currentTime.toString());

        int currentHour = currentTime.getHour();
        int currentMinute = currentTime.getMinute();
        int second = currentTime.getSecond();

        TimeOfDay timeOfDay = TimeOfDay.hourMinuteAndSecondOfDay(currentHour, currentMinute, second);
        DateTime startTime = DateUtil.offsetSecond(new Date(), 5);
        log.info("开始时间---{}", startTime);
        SimpleProperty simpleProperty = new SimpleProperty();
        simpleProperty.setStartTime(startTime);
        simpleProperty.setRepeatCount(0L);
        simpleProperty.setTimeOfDay(timeOfDay);
        sysJob.setSimpleProperty(simpleProperty);

        ScheduleUtils.createExtractTopicScheduleJob(scheduler, sysJob, objectMapper);
    }


    @GetMapping("video")
    public void video() throws SchedulerException {
        SysJob sysJob = new SysJob();
        sysJob.setJobId("1");
        sysJob.setJobName("name");
        sysJob.setJobGroup("groupName");
        sysJob.setTargetClass("testJob");

        ObjectMapper objectMapper = new ObjectMapper();

        // 创建一个空的 JSON 对象
        ObjectNode jsonNode = objectMapper.createObjectNode();

        // 添加属性到 JSON 对象
        jsonNode.put("name", "John Doe");
        jsonNode.put("age", 30);
        jsonNode.put("isStudent", false);

//        LocalTime currentTime = LocalTime.now().plusSeconds(10L);
//        log.info("执行时间---{}",currentTime.toString());
//
//        int currentHour = currentTime.getHour();
//        int currentMinute = currentTime.getMinute();
//        int second = currentTime.getSecond();

//        TimeOfDay timeOfDay = TimeOfDay.hourMinuteAndSecondOfDay(currentHour, currentMinute,second);
        DateTime startTime = DateUtil.offsetSecond(new Date(), 5);
        log.info("开始时间---{}", startTime);
        SimpleProperty simpleProperty = new SimpleProperty();
        simpleProperty.setStartTime(startTime);
//        simpleProperty.setRepeatCount(0L);
//        simpleProperty.setTimeOfDay(timeOfDay);
        sysJob.setSimpleProperty(simpleProperty);

        ScheduleUtils.createVideoTopicScheduleJob(scheduler, sysJob, objectMapper);
    }


}
